package com.haircut.sys.utils.menu;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.haircut.sys.constant.Constants;
import com.haircut.sys.constant.UserConstants;
import com.haircut.sys.model.menu.SysMenu;
import com.haircut.sys.vo.menu.MenuTreeVo;
import com.haircut.sys.vo.menu.MenuVo;
import com.haircut.sys.vo.menu.RouterVo;
import com.modular.constant.NumberConstant;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

public class MenuUtils {


    public static List<RouterVo> buildMenus(List<MenuVo> menuList){
        List<RouterVo> routers = new LinkedList<RouterVo>();
        for (MenuVo menu : menuList)
        {
            RouterVo router = new RouterVo();
            router.setHidden(Objects.equals(NumberConstant.ONE,menu.getVisible()));
            router.setName(getRouteName(menu));
            router.setPath(getRouterPath(menu));
            router.setComponent(getComponent(menu));
            router.setQuery(menu.getQuery());
            router.setMeta(coverMeta(menu));
            List<MenuVo> cMenus = menu.getChildren();
            if (CollUtil.isNotEmpty(cMenus) && Objects.equals(menu.getMenuType(), UserConstants.MenuType.DIR))
            {
                router.setAlwaysShow(true);
                router.setRedirect("noRedirect");
                router.setChildren(buildMenus(cMenus));
            }
            else if (isMenuFrame(menu))
            {
                router.setMeta(null);
                List<RouterVo> childrenList = new ArrayList<RouterVo>();
                RouterVo children = new RouterVo();
                children.setPath(menu.getMenuUrl());
                children.setComponent(menu.getComponent());
                children.setName(StringUtils.capitalize(menu.getMenuUrl()));
                children.setMeta(coverMeta(menu));
                children.setQuery(menu.getQuery());
                childrenList.add(children);
                router.setChildren(childrenList);
            }
            else if (Objects.equals(Constants.DEFAULT_PARENT_ID,menu.getParentId())  && isInnerLink(menu))
            {
                router.setMeta(coverMeta(menu));
                router.setPath("/");
                List<RouterVo> childrenList = new ArrayList<RouterVo>();
                RouterVo children = new RouterVo();
                String routerPath = innerLinkReplaceEach(menu.getMenuUrl());
                children.setPath(routerPath);
                children.setComponent(UserConstants.ComponentType.INNER_LINK);
                children.setName(StringUtils.capitalize(routerPath));
                children.setMeta(coverMeta(menu));
                childrenList.add(children);
                router.setChildren(childrenList);
            }
            routers.add(router);
        }
        return routers;
    }

    /**
     * 获得meta
     * @param menu
     * @return
     */
    private static RouterVo.MetaVo coverMeta(MenuVo menu){
        RouterVo.MetaVo metaVo = new RouterVo.MetaVo();
        metaVo.setTitle(menu.getMenuName());
        metaVo.setIcon(menu.getMenuIcon());
        metaVo.setLink((HttpUtil.isHttp(menu.getMenuUrl()) || HttpUtil.isHttps(menu.getMenuUrl()))?menu.getMenuUrl():StrUtil.EMPTY);
        return metaVo;
    }

    /**
     * 获取组件信息
     *
     * @param menu 菜单信息
     * @return 组件信息
     */
    public static String getComponent(MenuVo menu)
    {
        String component = UserConstants.ComponentType.LAYOUT;
        if (StrUtil.isNotEmpty(menu.getComponent()) && !isMenuFrame(menu))
        {
            component = menu.getComponent();
        }
        else if (StrUtil.isEmpty(menu.getComponent()) && Objects.equals(Constants.DEFAULT_PARENT_ID,menu.getParentId()) && isInnerLink(menu))
        {
            component = UserConstants.ComponentType.INNER_LINK;
        }
        else if (StrUtil.isEmpty(menu.getComponent()) && isParentView(menu))
        {
            component = UserConstants.ComponentType.PARENT_VIEW;
        }
        return component;
    }

    /**
     * 是否为parent_view组件
     *
     * @param menu 菜单信息
     * @return 结果
     */
    public static boolean isParentView(MenuVo menu)
    {
        return !Objects.equals(Constants.DEFAULT_PARENT_ID,menu.getParentId()) && Objects.equals(menu.getMenuType(), UserConstants.MenuType.DIR) ;
    }

    /**
     * 获取路由地址
     *
     * @param menu 菜单信息
     * @return 路由地址
     */
    public static String getRouterPath(MenuVo menu)
    {
        String routerPath = menu.getMenuUrl();
        // 内链打开外网方式
        if (Objects.equals(menu.getParentId(), Constants.DEFAULT_PARENT_ID) && isInnerLink(menu))
        {
            routerPath = innerLinkReplaceEach(routerPath);
        }
        // 非外链并且是一级目录（类型为目录）
        if (Objects.equals(menu.getParentId(),Constants.DEFAULT_PARENT_ID) && Objects.equals(UserConstants.MenuType.DIR,menu.getMenuType())
                && menu.getIsFrame().equals(NumberConstant.ONE))
        {
            routerPath = "/" + menu.getMenuUrl();
        }
        // 非外链并且是一级目录（类型为菜单）
        else if (isMenuFrame(menu))
        {
            routerPath = "/";
        }
        return routerPath;
    }

    /**
     * 内链域名特殊字符替换
     *
     * @return 替换后的内链域名
     */
    public static String innerLinkReplaceEach(String path)
    {
//        return StrUtil.format(path, new String[] { Constants.HTTP, Constants.HTTPS, Constants.WWW, ".", ":" },
//                new String[] { "", "", "", "/", "/" });
        return path;
    }

    /**
     * 获取路由名称
     *
     * @param menu 菜单信息
     * @return 路由名称
     */
    public static String getRouteName(MenuVo menu)
    {
        String routerName = StrUtil.trim(menu.getMenuUrl());
        // 非外链并且是一级目录（类型为目录）
        if (isMenuFrame(menu))
        {
            routerName = StrUtil.EMPTY;
        }
        return routerName;
    }


    /**
     * 是否为菜单内部跳转
     *
     * @param menu 菜单信息
     * @return 结果
     */
    public static boolean isMenuFrame(MenuVo menu)
    {
        return Objects.equals(menu.getParentId(),String.valueOf(NumberConstant.ZERO)) && UserConstants.MenuType.MENU.equals(menu.getMenuType())
                && menu.getIsFrame().equals(NumberConstant.ONE);
    }
    /**
     * 是否为内链组件
     *
     * @param menu 菜单信息
     * @return 结果
     */
    public static boolean isInnerLink(MenuVo menu)
    {
        return menu.getIsFrame().equals(NumberConstant.ONE) && HttpUtil.isHttp(menu.getMenuUrl())  && HttpUtil.isHttps(menu.getMenuUrl());
    }

    /**
     * 转换数据格式
     * @param menuVoList
     * @return
     */
    public static List<MenuTreeVo> coverTree(List<MenuVo> menuVoList){
        List<MenuTreeVo> treeVoList = new ArrayList<>(menuVoList.size());
        MenuTreeVo treeVo = null;
        for(MenuVo menuVo:menuVoList){
            treeVo = new MenuTreeVo();
            treeVo.setId(menuVo.getId());
            treeVo.setLabel(menuVo.getMenuName());
            if(CollUtil.isNotEmpty(menuVo.getChildren())){
                treeVo.setChildren(coverTree(menuVo.getChildren()));
            }
            treeVoList.add(treeVo);
        }
        return treeVoList;
    }
}
